/*
 * Copyright 2017-2101 Innel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hykj.xxltest.controller;

import com.hykj.xxltest.base.AppBack;
import com.hykj.xxltest.model.UserVerifyModel;
import com.hykj.xxltest.service.UserService;
import com.hykj.xxltest.service.UserVerifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 验证码
 * @author innel
 * @email 2638086622@qq.com
 * @date 2018-03-14 17:09:08
 */
@Controller
@RequestMapping("/userverify")
@Api(description = "验证码")
public class UserVerifyController {

    private static Logger logger = Logger.getLogger(UserVerifyController.class);

    @Autowired
    private UserVerifyService userVerifyService;

    @Autowired
    private UserService userService;

    /**
     * 保存
     */
    @RequestMapping("/send")
    @ResponseBody
    @ApiOperation(
            value = "发送验证码",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack send(@ApiParam(value = "手机号", required = true)
                        @RequestParam(required = true)
                                String phone) {
        UserVerifyModel userVerify = new UserVerifyModel();
        userVerify.setStatus(0);
        userVerify.setPhone(phone);
        //  String code="1234";
        String code=(int) (Math.random()*9000+1000)+"";
        userVerify.setCode(code);
        userVerifyService.sendSDK(phone,code,"0","","");
        userVerifyService.create(userVerify);
        return new AppBack();
    }

    /**
     * 保存
     */
    @RequestMapping("/sendEmail")
    @ResponseBody
    @ApiOperation(
            value = "发送邮箱验证码",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack sendEmail(@ApiParam(value = "发送邮箱验证码", required = true)
                        @RequestParam(required = true)
                                String phone) {
        UserVerifyModel userVerify = new UserVerifyModel();
        userVerify.setStatus(0);
        userVerify.setPhone(phone);
        //  String code="1234";
        String code=(int) (Math.random()*9000+1000)+"";
        userVerify.setCode(code);
        userVerifyService.sendEmail(phone,code);
        userVerifyService.create(userVerify);
        return new AppBack();
    }



    /**
     * 列表带分页
     */
    @RequestMapping("/check")
    @ResponseBody
    @ApiOperation(
            value = "验证验证码是否正确",
            notes = "备注：验证码被验证后还是有效的",
            httpMethod = "POST")
    public AppBack list(@RequestParam String phone,
                        @RequestParam String code) {
        Map condition=new HashMap();
        condition.put("phone",phone);
        condition.put("code",code);
        List<UserVerifyModel> list = userVerifyService.getListLite(condition);
        if(list.size()==0){
            return new AppBack(-1,"验证码错误或过期");
        }
        return new AppBack();
    }

}
